// 菜单栏权限
const { MongoClient, ServerApiVersion } = require('mongodb')
let dayjs = require('dayjs')
const { generateToken } = require('../utils/getToken')

// 数据库地址
const uri = 'mongodb://localhost:27017/libraryDB'

// 连接数据库
const client = new MongoClient(uri, {
  serverApi: {
    version: ServerApiVersion.v1,
    strict: true,
    deprecationErrors: true,
  },
})

const menuRole = async (name) => {
  try {
    await client.connect()
    // 获取数据库对象
    const db = client.db('libraryDB')
    // 获取菜单枚举权限表
    // 获取菜单枚举权限表
    const roleRouterDb = db.collection('role_router')
    const res = await roleRouterDb.findOne(name)
    if (res) {
      return {
        msg: '获取菜单栏成功',
        time: dayjs().format('YYYY-MM-DD HH:mm:ss'),
        msgCode: 10000,
        data: {
          router: res.router,
          success: true,
        },
      }
    } else {
      return {
        msg: '没有登录权限，请联系管理员',
        time: dayjs().format('YYYY-MM-DD HH:mm:ss'),
        msgCode: 99999,
        data: {
          success: false,
        },
      }
    }
  } catch (error) {
    return {
      msg: '服务器异常',
      success: false,
    }
  }
}
module.exports = { menuRole }
